﻿using System.Collections.Generic;
using System.IO;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
using Commons.Collections;


namespace CARMSBusiness
{

    /// 
    /// NVelocity模板工具类 VelocityHelper
    /// 
    public class VelocityHelper
    {
        private VelocityEngine velocity = null;
        private IContext context = null;

        private Dictionary<string, object> _variables;

        /// 
        /// 构造函数
        /// 
        ///
        //模板文件夹路径
        public VelocityHelper(string templatDir)
        {
            Init(templatDir);
        }
        /// 
        /// 无参数构造函数
        /// 
        public VelocityHelper() { ;}
        /// 
        /// 初始话NVelocity模块
        /// 
        ///
        //模板文件夹路径
        public void Init(string templateDir)
        {
            //创建VelocityEngine实例对象
            velocity = new VelocityEngine();

            //使用设置初始化VelocityEngine
            ExtendedProperties props = new ExtendedProperties();
            props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir);
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312");
            props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312");
            velocity.Init(props);

            //为模板变量赋值
            context = new VelocityContext();
        }
        /// 
        /// 给模板变量赋值
        /// 
        ///
        //模板变量
        ///
        //模板变量值
        public void Put(string key, object value)
        {
            if (context == null)
                context = new VelocityContext();
            context.Put(key, value);
        }
        /// 
        /// 显示模板
        /// 
        ///
        //模板文件名
        public string GetContent(string templatFileName)
        {

            foreach (var pair in Variables)
            {
                context.Put(pair.Key, pair.Value);
            }

            //从文件中读取模板
            Template template = velocity.GetTemplate(templatFileName);
            //合并模板
            StringWriter writer = new StringWriter();
            template.Merge(context, writer);
            //输出 
            return writer.GetStringBuilder().ToString();
        }


        /// <summary>
        /// 获取模板中的所有变量。
        /// </summary>
        public Dictionary<string, object> Variables
        {
            get
            {
                if (_variables == null)
                {
                    _variables = new Dictionary<string, object>();
                }
                return _variables;
            }
        }

    }
}
